﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Wpf.Windows
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Type> Types = new List<Type>()
        {
            typeof(ControlsWindow),
            typeof(AboutWindow),
            typeof(PanelWindow),
            typeof(GridWindow),
            typeof(CanvasWindow),
            typeof(ContentControlWindow),
            typeof(DependencyRoutedWindow),
        };

        public MainWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            int i = 0;
            int mCols = mgrid.ColumnDefinitions.Count;
            foreach (var item in Types)
            {
                var btn = new Button()
                {
                    Content = item.Name,
                    Tag = item,
                    Margin = new Thickness(10d),
                };
                btn.Click += Btn_Click;
                mgrid.Children.Add(btn);
                Grid.SetColumn(btn, i % mCols);
                if (i >= mCols)
                    Grid.SetRow(btn, i / mCols);
                i++;
            }
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var type = btn.Tag as Type;
            var ob = AppDomain.CurrentDomain.CreateInstance(type.Assembly.FullName, type.FullName).Unwrap();
            if (ob is Window)
            {
                var win = ob as Window;
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                win.ShowDialog();
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            Info.RecordMethodInfo();
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            Info.RecordMethodInfo();
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
            Info.RecordMethodInfo();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Info.RecordMethodInfo();
            if (MessageBox.Show(this, "确认退出?", "退出", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
            {
                e.Cancel = true;
            }
            else
                e.Cancel = false;
        }

        protected override void OnClosed(EventArgs e)
        {
            Info.RecordMethodInfo();
        }
    }
}
/*
 

 */